package cn.tedu.websocket.demo.controller;

import cn.tedu.websocket.demo.pojo.WebSocketMessage;
import cn.tedu.websocket.demo.servers.WebSocketServer;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;

@RestController
public class WebSocketController {
    @Resource
    private WebSocketServer webSocketServer;
    /**
     * 接收一个请求 传递一个参数userId
     * 目标给这个用户id的客户端推送ws消息
     */
    @GetMapping("/send")
    public String send(Integer id, String msg) {
        WebSocketMessage<String> message = new WebSocketMessage<>();
        message.setState(200);
        message.setMessage("成功");
        message.setData(msg);
        webSocketServer.sendMessage(id, message);
        return "success";
    }
}
